package com.yanqu.road.server.cmd.tradewar.team;

import com.yanqu.road.entity.tradewar.UserTradeWarTeam;
import com.yanqu.road.pb.tradewar.team.TradeWarTeamProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.TradeWarTeamModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_TRADE_WAR_EDIT_TEAM, desc = "编辑商战队伍")
public class TradeWarEditTeamCmd extends AbstractCommand {

	private int index;
	private List<Integer> patronsIdList;
	private int teamType = 0;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		TradeWarTeamProto.TradeWarTeamEditReqMsg reqMsg = TradeWarTeamProto.TradeWarTeamEditReqMsg.parseFrom(packet.getBytes());
		index = reqMsg.getTeamIndex();
		patronsIdList = new ArrayList<>();
		for(Integer patronsId : reqMsg.getPatronsIdListList()){
			if(!patronsIdList.contains(patronsId)){
				patronsIdList.add(patronsId);
			}
		}
		if (reqMsg.hasTeamType()){
			teamType = reqMsg.getTeamType();
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ret = player.getModule(TradeWarTeamModule.class).editTradeWarTeam(index, patronsIdList, teamType);
	}

	@Override
	public void sendData(GamePlayer player) {
		TradeWarTeamProto.TradeWarTeamEditRespMsg.Builder respMsg = TradeWarTeamProto.TradeWarTeamEditRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret) {
			respMsg.setTeamType(teamType);
			respMsg.setTeamData(player.getModule(TradeWarTeamModule.class).getTeamData(teamType));
		}
		player.sendPacket(Protocol.U_TRADE_WAR_EDIT_TEAM, respMsg);
	}

}
